package org.smallreunion.controller.bbs;

import org.slim3.controller.Navigation;
import org.slim3.datastore.S3QueryResultList;
import org.slim3.util.RequestMap;
import org.smallreunion.controller.AbstractController;
import org.smallreunion.model.BBSPost;
import org.smallreunion.service.BBSService;

/**
 * BBSIndexController
 *
 * @author Sinmetal
 *
 */
public class IndexController extends AbstractController {

    private BBSService bbsService = new BBSService();

    /**
     * 掲示板トップページを表示
     */
    @Override
    public Navigation run() throws Exception {
        S3QueryResultList<BBSPost> bbsPostList =
            getBBSPostList(new RequestMap(request));
        requestScope("bbsPostList", bbsPostList);
        requestScope("cursor", bbsPostList.getEncodedCursor());
        requestScope("hasNext", bbsPostList.hasNext());
        return forward("index.jsp");
    }

    /**
     * 掲示板投稿取得
     *
     * @param requestMap
     * @return
     */
    protected S3QueryResultList<BBSPost> getBBSPostList(RequestMap requestMap) {
        final String HASHTAG = "hashtag";
        if (requestMap.containsKey(HASHTAG)) {
            String hashtag = requestMap.get(HASHTAG).toString();
            return bbsService.getBBSPostListByHashtag(hashtag);
        }
        return bbsService.getBBSPostList();
    }
}
